﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

using Microsoft.Win32;

using MineLaunch.Setting;

namespace MineLaunch.Windows
{
	/// <summary>
	/// SettingsWindow.xaml の相互作用ロジック
	/// </summary>
	public partial class SettingsWindow : Window
	{
		public SettingsWindow()
		{
			InitializeComponent();
		}

		private void Window_SourceInitialized(object sender, EventArgs e)
		{
			var settings = SettingsMain.GetInstance();
			this.IsAutoLoginCheckBox.IsChecked = settings.settingsMain.IsAutoLogin;
			this.MinecraftIDTextBox.Text = settings.settingsMain.MinecraftID;
			this.JavaPathTextBox.Text = settings.settingsMain.JavaPath;
			this.TargetDirectoryTextBox.Text = settings.settingsMain.TargetDirectory;
			this.MCPatcherPathTextBox.Text = settings.settingsMain.MCPatcherPath;
			this.XmsTextBox.Text = settings.settingsMain.Xms > 0 ? settings.settingsMain.Xms.ToString() : "";
			this.XmxTextBox.Text = settings.settingsMain.Xmx > 0 ? settings.settingsMain.Xmx.ToString() : "";
			this.MinecraftWidthTextBox.Text = settings.settingsMain.MinecraftWidth > 0 ? settings.settingsMain.MinecraftWidth.ToString() : "";
			this.MinecraftHeightTextBox.Text = settings.settingsMain.MinecraftHeight > 0 ? settings.settingsMain.MinecraftHeight.ToString() : "";

			if (settings.settingsMain.MinecraftPassword != "")
			{
				this.MinecraftPasswordTextBox.Password = Common.Common.Decrypt(settings.settingsMain.MinecraftPassword);
			}

			foreach (var theme in (Application.Current as App).Themes)
			{
				themesComboBox.Items.Add(theme.Key);
			}
			themesComboBox.SelectedItem = settings.settingsMain.Theme;
		}

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			var settings = SettingsMain.GetInstance();
			settings.settingsMain.IsAutoLogin = this.IsAutoLoginCheckBox.IsChecked.Value;
			settings.settingsMain.MinecraftID = this.MinecraftIDTextBox.Text;
			settings.settingsMain.JavaPath = this.JavaPathTextBox.Text;
			settings.settingsMain.TargetDirectory = this.TargetDirectoryTextBox.Text;
			settings.settingsMain.MCPatcherPath = this.MCPatcherPathTextBox.Text;
			if (this.MinecraftPasswordTextBox.Password != "")
			{
				settings.settingsMain.MinecraftPassword = Common.Common.Encrypt(this.MinecraftPasswordTextBox.Password);
			}

			try
			{
				int xms = Convert.ToInt32(this.XmsTextBox.Text);
				settings.settingsMain.Xms = xms > 0 ? xms : SettingsGlobal.DefaultXms;
			}
			catch
			{
				settings.settingsMain.Xms = SettingsGlobal.DefaultXms;
			}
			try
			{
				int xmx = Convert.ToInt32(this.XmxTextBox.Text);
				settings.settingsMain.Xmx = xmx > 0 ? xmx : SettingsGlobal.DefaultXmx;
			}
			catch
			{
				settings.settingsMain.Xmx = SettingsGlobal.DefaultXmx;
			}

			try
			{
				int width = Convert.ToInt32(this.MinecraftWidthTextBox.Text);
				settings.settingsMain.MinecraftWidth = width > 0 ? width : SettingsGlobal.DefaultWidth;
			}
			catch
			{
				settings.settingsMain.MinecraftWidth = SettingsGlobal.DefaultWidth;
			}
			try
			{
				int height = Convert.ToInt32(this.MinecraftHeightTextBox.Text);
				settings.settingsMain.MinecraftHeight = height > 0 ? height : SettingsGlobal.DefaultHeight;
			}
			catch
			{
				settings.settingsMain.MinecraftHeight = SettingsGlobal.DefaultHeight;
			}

			if (settings.settingsMain.TargetDirectory == "")
			{
				settings.settingsMain.TargetDirectory = SettingsGlobal.DefaultTargetDirectory;
			}
			settings.settingsMain.Theme = themesComboBox.SelectedItem.ToString();

			this.DialogResult = true;
		}

		private void OpenJavaPathDialogButton_Click(object sender, RoutedEventArgs e)
		{
			var dialog = new OpenFileDialog();
			dialog.Title = "java.exeを指定";
			dialog.InitialDirectory = Path.GetDirectoryName(SettingsGlobal.DefaultJavaPath);
			dialog.FileName = Path.GetFileName(SettingsGlobal.DefaultJavaPath);
			dialog.Filter = "java.exe|java.exe";
			if (dialog.ShowDialog().Value)
			{
				JavaPathTextBox.Text = dialog.FileName;
			}
		}

		private void OpenTargetDirectoryDialogButton_Click(object sender, RoutedEventArgs e)
		{
			var dialog = new CommonDialog.FolderBrowserDialog();
			if (dialog.ShowDialog().Value)
			{
				TargetDirectoryTextBox.Text = dialog.SelectedPath;
			}
		}

		private void OpenMCPatcherPathButton_Click(object sender, RoutedEventArgs e)
		{
			var dialog = new OpenFileDialog();
			dialog.Title = "MCPatcherのパスを指定";
			dialog.InitialDirectory = Directory.GetCurrentDirectory();
			dialog.Filter = "*.exe|*.exe";
			if (dialog.ShowDialog().Value)
			{
				this.MCPatcherPathTextBox.Text = dialog.FileName;
			}
		}
	}
}
